﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using InstrumentCheck.Module;
using InstrumentCheck.SQLHelper;

namespace InstrumentCheck.DAL
{
    public class ReceiveBox_dal
    {
        public static List<ApplyForm> ReceiveData(string UserNO,int ApprovalStatus)
        {
            List<ApplyForm> applyform = new List<ApplyForm>();
            string sql = "select * from v_ReceiveBox where (ReceiverNO = @receiverno or AgentNO=@receiverno) and ApproveStatus = @approvalstatus Order By Number";
            SqlParameter[] para = new SqlParameter[2];
            para[0] = SQLHelper.SQLHelper.CreateParameter("receiverno", SqlDbType.NVarChar, UserNO, ParameterDirection.Input);
            para[1] = SQLHelper.SQLHelper.CreateParameter("approvalstatus", SqlDbType.Int, ApprovalStatus, ParameterDirection.Input);
            SqlDataAdapter ada = new SqlDataAdapter();
            DataSet ds = new DataSet();
            SQLHelper.SQLHelper.Fill(ada, ds, sql, CommandType.Text, para);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                    applyform.Add(getListApplyForm(row));
            }
            return applyform;
        }
        public static ApplyForm getListApplyForm(DataRow row)
        {
            ApplyForm tmpApplyForm = new ApplyForm();
            tmpApplyForm.FormID = int.Parse(row["correctid"].ToString());
            tmpApplyForm.FormNO = int.Parse(row["number"].ToString());
            tmpApplyForm.FormStatus = (FormStatus)row["FORMSTATUS"];
            tmpApplyForm.ApplyDate  = DateTime.Parse(row["CREATE_DATE"].ToString());
            User user = new User();
            user.EmpChineseName = row["USER_NAME"].ToString();
            tmpApplyForm.Applicant = user;
            Item item = new Item();
            item.AssetNO = row["FAS_NO"].ToString();
            item.ChiName = row["CHI_NAME"].ToString();
            item.ModelNO = row["MODEL_NO"].ToString();
            item.Spec = row["SPEC"].ToString();
            item.NeedCorrectDate = DateTime.Parse(row["RECEIVEDATE"].ToString());  
            tmpApplyForm.Item = item;
            List<FormRouting> formrouting = new List<FormRouting>();
            FormRouting routing = new FormRouting();
             routing.ApproveStatus = (ApproveStatus)row["ApproveStatus"];
             formrouting.Add(routing);
            tmpApplyForm.FormRoutingHistory = formrouting;
            return tmpApplyForm;
        }
    }

}
